from ..processors import *


class Normalize(NormalizeImage):
    """
    Normalize an image.

    Args:
        mean (list, optional): The mean value of a data set. Default: [0.5, 0.5, 0.5].
        std (list, optional): The standard deviation of a data set. Default: [0.5, 0.5, 0.5].

    Raises:
        ValueError: When mean/std is not list or any value in std is 0.
    """


    def __init__(self, mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)):
        if not (isinstance(mean, (list, tuple)) and isinstance(std, (list, tuple))):
            raise ValueError("{}: input type is invalid. It should be list or tuple".format(self))

        from functools import reduce
        if reduce(lambda x, y: x * y, std) == 0:
            raise ValueError('{}: std is invalid!'.format(self))

        super().__init__(mean, std, True, "mean_std")
        

    def __call__(self, im,im_info):
        im,im_info = super().__call__(im,im_info)
        return im,im_info

"""
seg的预处理, paddleSeg中将Permute直接写进了Compose中而不是独立成一个transformer ops, 
为了统一代码风格同时兼容paddleSeg, 我们弃用Compose类并将相关操作在preprocess函数中完成"""
def preprocess(im, preprocess_ops):
    # process image by preprocess_ops
    im_info = {
        'scale_factor': np.array(
            [1., 1.], dtype=np.float32),
        'im_shape': None,
    }
    im, im_info = decode_image(im, im_info)
    for operator in preprocess_ops:
        im,im_info = operator(im,im_info)
    im = np.transpose(im, (2, 0, 1))
    return im,im_info